<?php

namespace Application\Controller;

use Application\Controller\IndexController;
use Zend\Form\Annotation\AnnotationBuilder;
use Application\Form\SearchDocumentForm;
use Admin\Model\DBO\SearchBookDBO;
use Application\Model\DBO\BookingOnlineDBO;

class SearchBookCategoryController extends IndexController {

    protected $form;

    public function getForm() {
        if (!$this->form) {
            $searchDocument = new SearchDocumentForm();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($searchDocument);
            $valueOptions = array(
                '1' => 'Nhan đề',
                '2' => 'Tác giả',
                '3' => 'Nhà xuất bản',
                '4' => 'Năm năm xuất bản',
            );
            $this->form->get('keySearch')->setOptions(
                    array(
                        'value_options' => $valueOptions,
                    )
            );
        }

        return $this->form;
    }

    public function indexAction() {
        $this->layout('layout/layout.phtml');
        $valueSearch = $this->params()->fromRoute('id', 0);
        $form = $this->getForm();
        $objectSearchBook = new SearchBookDBO($this->getServiceLocator());
        $resultSet = $objectSearchBook->searchBookCategory($valueSearch);
        // Tim kiem them ma tac gia
        $allAuthor = null;
        $myMessage = array();
        foreach ($resultSet as $key) {
            $bookBarcode = $key['bookBarcode'];
            $authors = $objectSearchBook->searchBookAuthor($bookBarcode);
            $i = 1;
            $strAuthor = "";
            foreach ($authors as $author) {
                $strAuthor .= '-' . $author->author;
            }
            $allAuthor[$bookBarcode] = $strAuthor;
        }
        $paginator = null;
        if (isset($bookBarcode) && $bookBarcode != null) {
            $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($objectSearchBook->searchBookCategory($valueSearch));
            $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
            $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
            $paginator->setItemCountPerPage(15);
            $paginator->setPageRange(5);
            // set the number of items per page to 10
        }
        if ($paginator == null) {
            $myMessage['notFoundResult'] = 'notFoundResult';
        }
        return array(
            'form' => $form,
            'myMessage' => $myMessage,
            'paginator' => $paginator,
            'allAuthor' => $allAuthor,
        );
    }

    public function detailBookAction() {
        $this->layout('layout/layout.phtml');
        $bookBarcode = $this->params()->fromRoute('id', 0);
        $form = $this->getForm();
        $objectSearchBook = new SearchBookDBO($this->getServiceLocator());
        $data = $objectSearchBook->searchBookDetail($bookBarcode);
        return array_merge($data, array('form' => $form));
    }

    public function bookingOnlineAction() {
        // Nêu bạn đọc đã đăng nhập thì mươn con không thì quay về trang đăng nhập
        if ($this->checkRole()) {
            // chuan bi dua du lieu vao bang order
            $cardBarcode = $this->getAuthService()->getIdentity();
            $param = $this->params()->fromRoute('id', 0);
            $bookBarcode = "";
            $bookID = "";
            if ($param != 0) {
                $toArray = explode('-', $param);
                if (isset($toArray[1])) {
                    $bookBarcode = $toArray[0];
                    $bookID = $toArray[1];
                }
            }
            $objectBookingOnlineDBO = new BookingOnlineDBO($this->getServiceLocator());
            if ($objectBookingOnlineDBO->OrderBook($cardBarcode, $bookID)) {
                return $this->redirect()->toRoute('personal/process',array('action'=>'orderOnline'));
            } else {
                $this->layout('layout/layout.phtml');
                $myMessage = "Error";
                $form = $this->getForm();
                $objectSearchBook = new SearchBookDBO($this->getServiceLocator());
                $data = $objectSearchBook->searchBookDetail($bookBarcode);
                return array_merge($data, array('form' => $form, 'myMessage' => $myMessage,));
            }
        }
        $this->redirect()->toRoute('home');
    }

}